package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.*;
import org.jiuling.aliyun.sms.enums.TemplateTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsAsynchronous;
import org.jiuling.aliyun.sms.factory.service.AliYunTemplateManagerService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunTemplateManagerVerify;
import org.jiuling.aliyun.sms.model.*;

import java.util.concurrent.ExecutionException;

/**
 * 阿里云异步模板管理
 * @author 紫灵
 * @date 2023年02月10日 22时34分56秒
 */
public class AliYunAsyncTemplateManagerServiceImpl extends AliYunSmsAsynchronous implements AliYunTemplateManagerService {

    public AliYunAsyncTemplateManagerServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddSmsTemplateResponseModel addSmsTemplate(TemplateTypeEnum templateType, String templateName, String templateContent,String remark) {
        AliYunTemplateManagerVerify.addSmsTemplateParam(templateType, templateName, templateContent, remark);
        AddSmsTemplateRequest addSmsTemplateRequest = AddSmsTemplateRequest.builder()
                .templateType(templateType.getValue())
                .templateName(templateName)
                .templateContent(templateContent)
                .remark(remark)
                .build();
        try (AsyncClient asyncClient = create()){
            return AddSmsTemplateResponseConvert.bodyToModel(asyncClient.addSmsTemplate(addSmsTemplateRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteSmsTemplateResponseModel deleteSmsTemplate(String templateCode) {
        AliYunTemplateManagerVerify.deleteSmsTemplateParam(templateCode);
        DeleteSmsTemplateRequest deleteSmsTemplateRequest = DeleteSmsTemplateRequest.builder()
                .templateCode(templateCode)
                .build();
        try (AsyncClient asyncClient = create()){
            return DeleteSmsTemplateResponseConvert.bodyToModel(asyncClient.deleteSmsTemplate(deleteSmsTemplateRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public ModifySmsTemplateResponseModel modifySmsTemplate(TemplateTypeEnum templateType, String templateName, String templateCode, String templateContent, String remark) {
        AliYunTemplateManagerVerify.modifySmsTemplateParam(templateType, templateName, templateCode, templateContent, remark);
        ModifySmsTemplateRequest modifySmsTemplateRequest = ModifySmsTemplateRequest.builder()
                .templateType(templateType.getValue())
                .templateName(templateName)
                .templateCode(templateCode)
                .templateContent(templateContent)
                .remark(remark)
                .build();
        try (AsyncClient asyncClient = create()){
            return ModifySmsTemplateResponseConvert.bodyToModel(asyncClient.modifySmsTemplate(modifySmsTemplateRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsTemplateListResponseModel querySmsTemplateList(int pageNum, int pageSize) {
        QuerySmsTemplateListRequest querySmsTemplateListRequest = QuerySmsTemplateListRequest.builder()
                .pageIndex(pageNum)
                .pageSize(pageSize)
                .build();
        try (AsyncClient asyncClient = create()){
            return QuerySmsTemplateListResponseConvert.bodyToModel(asyncClient.querySmsTemplateList(querySmsTemplateListRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsTemplateResponseModel querySmsTemplate(String templateCode) {
        AliYunTemplateManagerVerify.querySmsTemplateParam(templateCode);
        QuerySmsTemplateRequest querySmsTemplateRequest = QuerySmsTemplateRequest.builder()
                .templateCode(templateCode)
                .build();
        try (AsyncClient asyncClient = create()){
            return QuerySmsTemplateResponseConvert.bodyToModel(asyncClient.querySmsTemplate(querySmsTemplateRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
